﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_cms_Banner : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["timeout"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                LoadQC();
            }

        }
    }
    protected void bt_clickthem(object sender, EventArgs e)
    {

        string ten = ITNLip.SqlInjectionOK(txtTenQC.Text);
        int vitri = Convert.ToInt32(drViTri.SelectedValue.ToString());
        string namevitri = drViTri.SelectedItem.Text;
        string link = txtLink.Text;
     

        string filename = FileUpload1.FileName;


        if (filename != "")
        {
            FileUpload1.SaveAs(Server.MapPath("~/files_upload/images/quangcao/" + filename));
            string sql = " insert into banner(Name,Image,Link,ViTri,NameVitri) values(N'" + ten + "','" + filename + "','" + link + "','" + vitri + "',N'"+namevitri+"') ";
            DataAccess.ExeNonSQL(sql);
            LoadQC();
        }
        else
        {


            string sql1 = " insert into banner(Name,ViTri,Link,NameVitri) values(N'" + ten + "','" + vitri + "','" + link + "',N'" + namevitri + "') ";
            DataAccess.ExeNonSQL(sql1);
            LoadQC();
        }
        Response.Write("<script>alert('Bạn đã thêm mới thành công')</script>");

    }
    protected void btchange(object sender, CommandEventArgs e)
    {
        string IdQC = e.CommandArgument.ToString();
        string ten = e.CommandName.ToString();
        if (ten == "X")
        {
            string sql1 = "delete from banner where IdQC='" + IdQC + "'";
            DataAccess.ExeNonSQL(ITNLip.SqlInjectionOK(sql1));
            Response.Write("<script>alert('Bạn chắc chắn muốn xóa một bản ghi này ! ') </script>");
            LoadQC();
        }
        else
        {
            Button1.Visible = true;
            Session.Add("IdQC", IdQC);
            DataTable dt = new DataTable();
            string sql2 = "select * from banner where IdQC='" + IdQC + "'";
            dt = DataAccess.ExeSQLToDataTable(ITNLip.SqlInjectionOK(sql2));
            if (dt.Rows.Count > 0)
            {
                txtTenQC.Text = dt.Rows[0][1].ToString();
                txtLink.Text = dt.Rows[0][4].ToString();
                
            }

        }
    }
    protected void bt_suaclick(object sender, EventArgs e)
    {
        string ten = ITNLip.SqlInjectionOK(txtTenQC.Text);
        int vitri = Convert.ToInt32(drViTri.SelectedValue.ToString());
        string link = txtLink.Text;
      
        string filename = FileUpload1.FileName;
        string namevitri = drViTri.SelectedItem.Text;
        string idqc = Session["IdQC"].ToString();
        if (filename == "")
        {
            string sql3 = "update banner set  Name=N'" + ten + "',Link='" + link + "',ViTri=N'" + vitri + "',NameViTri=N'"+namevitri+"' where IdQC='" + idqc + "' ";
            DataAccess.ExeNonSQL(sql3);
            LoadQC();
        }
        else
        {
            FileUpload1.SaveAs(Server.MapPath("~/files_upload/images/quangcao/" + filename));
            string sql4 = "update banner set  Name=N'" + ten + "',Link='" + link + "',ViTri=N'" + vitri + "',Image='" + filename + "',NameViTri=N'" + namevitri + "' where IdQC='" + idqc + "' ";
            DataAccess.ExeNonSQL(sql4);
            LoadQC();
        }
        Response.Write("<script>alert('Bạn sửa dữ liệu thành công')</script>");

    }
    protected void LoadQC()
    {
        string sqlload = "select * from banner";
        DataTable dt = new DataTable();
        dt = DataAccess.ExeSQLToDataTable(sqlload);
        if (dt.Rows.Count > 0)
        {
            dtlQC.DataSource = dt.DefaultView;
            dtlQC.DataBind();
        }
    }
    protected void btthaydoi(object sender, GridViewPageEventArgs e)
    {
        dtlQC.PageIndex = e.NewPageIndex;
        this.LoadQC();
    }
}